package com.changgou.goods.controller;

import com.changgou.goods.pojo.Template;
import com.changgou.goods.service.TemplateService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/template")
@CrossOrigin
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    /****
     * 查找所有模板列表
     * @return
     */
    @GetMapping
    public Result<List<Template>> findAll() {
        List<Template> templates = templateService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", templates);
    }

    /****
     * 添加模板
     * @param template
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Template template) {
        templateService.add(template);
        return new Result<>(true, StatusCode.OK, "添加成功");
    }

    /****
     * 条件查询所有
     * @param template
     * @return
     */
    @PostMapping("/search")
    public Result<List<Template>> findList(@RequestBody(required = false) Template template) {
        List<Template> templates = templateService.findList(template);
        return new Result<>(true, StatusCode.OK, "查询成功", template);
    }

    /****
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable Integer page, @PathVariable Integer size) {
        PageInfo<Template> pageInfo = templateService.findPage(page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /****
     * 分页条件查询
     * @param page
     * @param size
     * @param template
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable Integer page, @PathVariable Integer size,
                                     @RequestBody(required = false) Template template) {
        PageInfo<Template> pageInfo = templateService.findPage(page, size, template);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /****
     * 根据Id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Template> findById(@PathVariable Integer id) {
        Template template = templateService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", template);
    }

    /****
     * 根据Id修改
     * @param id
     * @param template
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id, @RequestBody Template template) {
        template.setId(id);
        templateService.update(template);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }

    /****
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        templateService.delete(id);
        return new Result<>(true, StatusCode.OK, "删除成功");
    }

    /***
     * 根据分类查询模板数据
     * @param id:分类ID
     */
    @GetMapping(value = "/category/{id}")
    public Result<Template> findByCategoryId(@PathVariable(value = "id") Integer id) {
        //调用Service查询
        Template template = templateService.findByCategoryId(id);
        return new Result<Template>(true, StatusCode.OK, "查询成功", template);
    }
}
